// 导入依赖
import * as FileSaver from 'file-saver';
import moment from 'moment';
import * as XLSX from 'xlsx';
 
/**
 * 导出Excel表格
 * @param name 生成excel的文件名，如：interestTable.xlsx
 * @param tableName 表格的id，如：#tableId
 * */
export const exportExcel = (name, tableName) => {
    let sel = XLSX.utils.table_to_book(document.querySelector(tableName), {raw: true})
    console.log('sel', sel)
    let selIn = XLSX.write(sel, { bookType: 'xlsx', bookSST: true, type: 'array' })
    try {
        FileSaver.saveAs(new Blob([selIn], { type: 'application/octet-stream' }), name)
    } catch (e) {
        if (typeof console !== 'undefined') console.log(e, selIn)
    }
 
    return selIn
}
export const handleExportTableData= (arrList) => {
    // 这里的needDownloadColumns要和Header 的列名字保持一致
      const Header = [
        [
          "姓名",
          "手机号码",
          "上报时间",
          "图片",
          "视频",
          "隐患地点",
        ]
      ];
      // 将JS数据数组转换为工作表。
      const headerWs = XLSX.utils.aoa_to_sheet(Header);
      const ws = XLSX.utils.sheet_add_json(headerWs, arrList, {
        skipHeader: true,
        origin: "A2"
      });
      // 新建空的工作表 
      const wb = XLSX.utils.book_new();
      // 可以自定义下载之后的sheetname
      XLSX.utils.book_append_sheet(wb, ws, "sheetName");
  
      // 生成xlsx文件
      const filename = 'Hidden_Danger_' + moment().format('YYYYMMDD')+'.xlsx'
      XLSX.writeFile(wb, filename, { type: "binary" });
    }